﻿package com.dg.gaming.client{
	import com.dg.gaming.api.common.AbstractConnectionThread;
	import com.dg.gaming.api.common.Game;
	import com.dg.gaming.api.common.msg.MsgGameMessage;
	import com.dg.gaming.api.common.msg.MsgTableMessage;
	import com.dg.gaming.api.common.Table;
	import com.dg.gaming.api.common.TableList;
	import com.dg.gaming.api.common.UserList;

	/**
	 * ...
	 * @author ...
	 */
	public class TableConnectionThread {
		/**
		 * Link to the server.
		 */
		private var conn:TableConnection;

		/**
		 * Table number.
		 */
		private var tableNum:String;

		/**
		 * Constructor which takes a ClientConnectionThread and a table number.
		 *
		 * @param conn      Connection to server.
		 * @param tableNum  Current table number.
		 */
		public function TableConnectionThread(conn:TableConnection,tableNum:String) {
			this.conn=conn;
			this.tableNum=tableNum;
		}

		/**
		 * @param message
		 */
		public function send(message:MsgTableMessage):void {
			message.setTableNum(this.tableNum);

			this.conn.send(message);
		}

		/**
		 * Return the table number.
		 *
		 * @return  Table number
		 */
		public function getTableNum():String {
			return this.tableNum;
		}

		/**
		 * Return the table associated with this table connection class.
		 *
		 * @param message
		 */
		public function getTable():Table {

			if (conn is TableConnection) {
				return TableConnection(conn).getTable();
			}

			return null;
		}

		/**
		 * Return the game object on this connection.
		 *
		 * @return
		 */
		public function getGame():Game {
			return conn.getTable().getGame();
		}

		// Delegate methods from ClientConnectionThread

		/**
		 * Send a game message.
		 *
		 * @param message   Game message.
		 */
		public function sendGameMessage(message:MsgGameMessage):void {
			this.conn.sendGameMessage(message);
		}

		/**
		 * Delegate method for returning a username;
		 *
		 * @return   Username.
		 */
		public function getUsername():Number {
			return this.conn.getUserId();
		}

		/**
		 * Delegate method for returning the table list.
		 *
		 * @return  List of tables.
		 */
		public function getTableList():TableList {

			return getGame().getTableList();
		}

		/**
		 * Delegate method for returning the user list.
		 *
		 * @return  List of users.
		 */
		public function getUserList():UserList {
			return getGame().getUserList();
		}

		/**
		 * Return thue if this user is the owner of the table.
		 *
		 * @return
		 */
		public function isOwnerOfTable():Boolean {
			return this.conn.getUserId()==getTable().getOwner();
		}

		/**
		 * Return the client connection thread which is a field.
		 *
		 * @return  Current connection to server.
		 */
		public function getClientConnectionThread():AbstractConnectionThread {
			return this.conn;
		}

		/**
		 * getUserid
		 *
		 * @return long
		 */
		public function getUserid():Number {
			return this.conn.getUserId();
		}
	}

}